﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class crappyPresent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        vote1.Click += new ImageClickEventHandler(vote_Click);
        vote2.Click += new ImageClickEventHandler(vote_Click);
        vote3.Click += new ImageClickEventHandler(vote_Click);
        vote4.Click += new ImageClickEventHandler(vote_Click);
        vote5.Click += new ImageClickEventHandler(vote_Click);
        vote6.Click += new ImageClickEventHandler(vote_Click);
        vote7.Click += new ImageClickEventHandler(vote_Click);
        vote8.Click += new ImageClickEventHandler(vote_Click);
        vote9.Click += new ImageClickEventHandler(vote_Click);
        vote10.Click += new ImageClickEventHandler(vote_Click);

        if (!IsPostBack) {
            if (Request.Cookies["presentVote"] != null && Request.Cookies["presentVote"].Value == "true") {
                Chimes.DataContext dataContext = new Chimes.DataContext();
                Chimes.Vote vote = dataContext.Votes.SingleOrDefault();
                if (vote != null) {
                    ShowPercentage(vote);
                }
            }
        }
    }

    void vote_Click(object sender, ImageClickEventArgs e)
    {
        if (IsValid){
            Chimes.DataContext dataContext = new Chimes.DataContext();
            Chimes.Vote vote = dataContext.Votes.SingleOrDefault();
            if (vote == null) {
                vote = new Chimes.Vote();
                dataContext.Votes.InsertOnSubmit(vote);
            }
            if (sender == vote1) {
                   vote.Img1++;
            }
            else if (sender == vote2)
            {
                vote.Img2++;
            }
            else if (sender == vote3)
            {
                vote.Img3++;
            }
            else if (sender == vote4)
            {
                vote.Img4++;
            }
            else if (sender == vote5)
            {
                vote.Img5++;
            }
            else if (sender == vote6)
            {
                vote.Img6++;
            }
            else if (sender == vote7)
            {
                vote.Img7++;
            }
            else if (sender == vote8)
            {
                vote.Img8++;
            }
            else if (sender == vote9)
            {
                vote.Img9++;
            }
            else if (sender == vote10)
            {
                vote.Img10++;
            }
            dataContext.SubmitChanges();

            HttpCookie voteCookie = new HttpCookie("presentVote", "true");
            
            Response.Cookies.Add(voteCookie);
            ShowPercentage(vote);              
        }
        
    }

    private void ShowPercentage(Chimes.Vote vote) {
        float total = vote.Img1 + vote.Img2 + vote.Img3 + vote.Img4 + vote.Img5 + vote.Img6 + vote.Img7 + vote.Img8 + vote.Img9 + vote.Img10;
        
        vote1.Visible = false;
        lblp1.Visible = true;
        lblp1.Text = ((float)vote.Img1 / total).ToString("#0%");

        vote2.Visible = false;
        lblp2.Visible = true;
        lblp2.Text = ((float)vote.Img2 / total).ToString("#0%");

        vote3.Visible = false;
        lblp3.Visible = true;
        lblp3.Text = ((float)vote.Img3 / total).ToString("#0%");

        vote4.Visible = false;
        lblp4.Visible = true;
        lblp4.Text = ((float)vote.Img4 / total).ToString("#0%");

        vote5.Visible = false;
        lblp5.Visible = true;
        lblp5.Text = ((float)vote.Img5 / total).ToString("#0%");

        vote6.Visible = false;
        lblp6.Visible = true;
        lblp6.Text = ((float)vote.Img6 / total).ToString("#0%");

        vote7.Visible = false;
        lblp7.Visible = true;
        lblp7.Text = ((float)vote.Img7 / total).ToString("#0%");

        vote8.Visible = false;
        lblp8.Visible = true;
        lblp8.Text = ((float)vote.Img8 / total).ToString("#0%");

        vote9.Visible = false;
        lblp9.Visible = true;
        lblp9.Text = ((float)vote.Img9 / total).ToString("#0%");

        vote10.Visible = false;
        lblp10.Visible = true;
        lblp10.Text = ((float)vote.Img10 / total).ToString("#0%");

        pnlEntry.Visible = false;
        pnlThankyou.Visible = true;
    }
}
